<template>
	<view class="detail-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" title="申请调整"></uni-navbar></view>

		<view class="detail-info">
			<view class="info-title">
				<view class="title-top">{{ title }}</view>
			</view>
		</view>

		<view class="form-box">
			<u-form :model="form" ref="uForm">
				<u-form-item class="form-item" label="调整单位">
					<u-input v-model="zbName" type="select" :select-open="zbShow" placeholder="请选择调整单位" @click="zbShow = true" />
					<select-tree ref="zbTree" :show="zbShow" :range="zbList" :selected="zbStr" rangeKey="deptName" :multiple="false" :selectParent="false" @confirm="treeConfirm($event, 'zb')" @cancel="treeCancel($event, 'zb')"></select-tree>
				</u-form-item>
				<u-form-item class="form-item" required label="调整理由" label-position="top" prop="returnRemark"><u-input v-model="form.returnRemark" type="textarea" input-align="left" placeholder="请输入调整理由" :clearable="false" :height="160" /></u-form-item>
			</u-form>

			<view class="fiexd-btm" v-if="$sysBtns('tagtqk')"><u-button type="error" @click="confirm">提交</u-button></view>
		</view>
	</view>
</template>

<script>
import selectTree from '@/components/uni-select-tree.vue';
import { mapState } from 'vuex';
export default {
	components: {
		selectTree
	},
	data() {
		return {
			title: '',

			zbIds: [],
			zbName: '',
			zbShow: false,
			zbList: [],
			zbStr: '',

			form: {
				motionId: null,
				applyUser: null,
				suggestDept: null,
				returnRemark: null
			},
			rules: {
				returnRemark: [
					{
						required: true,
						message: '请输入调整理由',
						trigger: 'change'
					}
				]
			},

			play: false
		};
	},
	onLoad(option) {
		this.loadLogin();
		this.title = option.title;
		if (option && option.motionId) {
			this.form.motionId = option.motionId;
			this.form.deptId = option.deptId;
		}

		this.loadHandler();
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;

				this.form.applyUser = this.userId;
			} else {
				this.$logout();
			}
		},
		loadHandler() {
			this.$request('/system/sys-dept/allHandler2', {
				clientCode: this.clientCode,
				currentUserId: this.userId,
				sanshenFlag: '1'
			}).then((res) => {
				this.zbList = res.data;
			});
		},
		treeConfirm(e, type) {
			if (!e || e.length == 0) {
				this[type + 'Show'] = false;
				this[type + 'Name'] = '';
				this[type + 'Ids'] = [];
				return false;
			}
			if (type == 'zb') {
				this[type + 'Ids'] = [e[0].id];
				this[type + 'Name'] = e[0].deptName;
			} else {
				this[type + 'Ids'] = [];
				let names = [];
				let i = 0;
				e.forEach((item) => {
					this[type + 'Ids'].push(item.id);
					if (i < 2) {
						names.push(item.deptName);
						i++;
					}
				});
				this[type + 'Name'] = names.join(',') + '等' + e.length + '个';
			}
			this[type + 'Show'] = false;
		},
		confirm() {
			this.$refs.uForm.validate((valid) => {
				if (!valid) {
					return false;
				} else {
					if (this.zbIds.length == 0) {
						this.$msg('调整单位不能为空');
						return false;
					}

					this.form.suggestDept = this.zbIds.join(',');

					let data = {};
					data = {
						clientCode: this.clientCode,
						currentUserId: this.userId,
						fileList: this.fileList,
						motionHandlerChange: this.form
					};
					this.$request('/motion/yian/handler/change/apply/return', data).then((res) => {
						this.$msg(res.msg);
						setTimeout(() => {
							let pre = this.$prePage();
							if (pre && pre.loadData) {
								pre.loadData('refresh');
							}
							uni.navigateBack();
						}, 500);
					});
				}
			});
		}
	}
};
</script>

<style lang="scss">
.detail-view {
	background-color: #fff;
}

.detail-info {
	.info-title {
		margin-bottom: 0;
	}
}

.info-h2 {
	padding: 0 30upx;
	font-size: 17px;
	font-weight: bold;
	color: #ce1617;
	line-height: 88upx;
	background-color: #f9fafc;
}

.info-inner {
	display: flex;

	.item {
		width: 50%;
	}
}

.form-box {
	.form-item {
		@include mp-flex;
		border-top: 20upx solid #f5f7fa;
	}
}

.fiexd-btm {
	position: static;
	padding: 30upx;
}
</style>
